package com.powernode.config;

/**
 * @version 1.0
 * @Date 2024/8/15 9:46
 * @Author 付~~~
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * redis缓存配置类
 */
@Configuration
public class RedisCacheConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(){
        // 创建一个redis的缓存配置类
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();

        // 进行redis配置
        redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json())) //redis中值的序列化方式改为json
                .entryTtl(Duration.ofDays(7))   //配置redis统一的默认过期时间
                .disableCachingNullValues();    //redis的value值禁止使用空值
        return redisCacheConfiguration;
    }
}
